package com.bbx.taxi.client.DB.Util;

import com.bbx.taxi.client.MyApplication;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * 
 * @author yanxuwen
 */
public class DBHelper extends SQLiteOpenHelper implements DBcolumns {

	private static final String DB_NAME = "bbxpc";
	private static final int DB_VERSION = 27;
	public static DBHelper mInstance;
    private static final String DROP_TABLE = "drop table if exists %s";

	/**
	 * 地址
	 */
	String sql_address = "CREATE TABLE " + TABLE_ADDRESS + " ("
			+ ADDRESS_ID + " INTEGER primary key autoincrement, " + " "
			+ ADDRESS_NAME + " text," + ADDRESS_ADDRESS + " text,"
			+ ADDRESS_LONGITUDE + " text," + ADDRESS_LATITUDE + " text,"+ ADDRESS_CITY + " text,"
			+ ADDRESS_COLLECTION + " text)";
	/**
	 * 历史联系人
	 */
	String sql_contacts = "CREATE TABLE " + TABLE_CONTACTS + " ("
			+ CONTACTS_ID + " INTEGER primary key autoincrement, " + " "
			+ CONTACTS_NAME + " text," + " " + CONTACTS_COLLECTION
			+ " text," + CONTACTS_TEL + " text)";
	/**
	 * 消息
	 */
	String sql_offline = "CREATE TABLE " + TABLE_MESSAGE + " ("
			+ MESSAGE_ID + " INTEGER primary key autoincrement, "
			+ " " + MESSAGE_ORDER_ID + " text," + MESSAGE_TIME + " text," + " "
			+ MESSAGE_TYPE + " INTEGER,"  + MESSAGE_READ + " text,"+ MESSAGE_JSON + " text)";
	/**
	 * 优惠劵
	 */
	String sql_coupon = "CREATE TABLE " + TABLE_COUPON + " (" + COUPON_id
			+ " INTEGER primary key autoincrement, " + " " + COUPON_ID
			+ " text," + COUPON_SUM + " text," + " " + COUPON_EXPIRE_TIME
			+ " text," + COUPON_USABLETIME + " text," + COUPON_USABLETYPE
			+ " text," + COUPON_USABLEORDER + " text," + COUPON_USABLELINE
			+ " text)";
	/**
	 * 推荐
	 */
	String sql_recomen = "CREATE TABLE " + TABLE_RECOMMEN + " (" + RECOMMEN_id
			+ " INTEGER primary key autoincrement, " + " " + RECOMMEN_PHONE
			+ " text," + RECOMMEN_NANE + " text," + RECOMMEN_STATE + " text," +  RECOMMEN_HEADIMG
			+ " text)";	
	/**订单取消*/
	String sql_order_cencel = "CREATE TABLE "
			+ TABLE_ORDER_CANCEL + " (" + CENCEL_ID
			+ " INTEGER primary key autoincrement, " + " "
			+ CENCEL_ORDER_ID + " text," + " "
			+ CANCEL_REASON + " text)";
	/**线路*/
	String sql_line = "CREATE TABLE "
			+ TABLE_LINE + " (" + LINE_ID
			+ " INTEGER primary key autoincrement, " + " "
			+ LINE_NAME + " text," + " "
			+ LINE_CITY + " text," + " "
			+ LINE_PROVINCE + " text," + " "
			+ LINE_INCITY + " text," + " "
			+ LINE_JSON + " text)";
	
	/**进行中的订单*/
	String sql_ordering = "CREATE TABLE "
			+ TABLE_ORDERING + " (" + ORDER_ID
			+ " INTEGER primary key autoincrement, " + " "
			+ MYORDER_ID + " text," + " "
			+ MYORDER_STATUS + " INTEGER," + " "
			+ MYORDER_JSON + " text)";
	/**全部订单*/
	String sql_order = "CREATE TABLE "
			+ TABLE_ORDER + " (" + ORDER_ID
			+ " INTEGER primary key autoincrement, " + " "
			+ MYORDER_ID + " text," + " "
			+ MYORDER_STATUS + " INTEGER," + " "
			+ MYORDER_JSON + " text)";
	
	public DBHelper(Context context,String uid) {
		super(context, DB_NAME+"_"+uid, null, DB_VERSION);
	}
	public DBHelper(Context context) {
		super(context, DB_NAME, null, DB_VERSION);
	}
	public DBHelper(Context context, int version,String uid) {
		super(context, uid+"_"+DB_NAME, null, version);
	}

	public synchronized static DBHelper getInstance(Context context,String uid) {
		mInstance=null;
		if (mInstance == null) {
			if(uid!=null&&!uid.equals("")){
				mInstance = new DBHelper(context,DB_VERSION,uid);
			}
			else{
				mInstance = new DBHelper(context);
			}
			
		}
		return mInstance;
	};

	@Override
	public void onCreate(SQLiteDatabase db) {

		
		db.execSQL(sql_address);// 地址
		db.execSQL(sql_contacts);// 常用联系人
		db.execSQL(sql_offline);// 离线消息
		db.execSQL(sql_coupon);// 优惠劵
		db.execSQL(sql_recomen);// 推荐
		db.execSQL(sql_order);// 我的订单
		db.execSQL(sql_ordering);// 我的进行中订单的
		db.execSQL(sql_order_cencel);//取消订单
		db.execSQL(sql_line);//线路
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.e("",oldVersion+",,,"+newVersion);
         if(oldVersion<=DB_VERSION){
        	 String DROP_TABLE_LINE= String.format(DROP_TABLE, TABLE_LINE);
        	 db.execSQL(DROP_TABLE_LINE);
        	 Log.e("","升级数据库");
     		db.execSQL(sql_line);
         } 
	}

}
